﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;



namespace TIoES
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // Object
        Texture2D rock;
        Vector2 vObjRock;         // 객체 위치

        // Mouse Controll
        MouseState mouseState, PreviousmouseState;
        

        // Game_Scene Switch
        enum Game_Scene { OPENING, MAIN, SELECT, PLAYING, RESULT, HELP, RANK };
        Game_Scene scene;

        // UI Scene
        Texture2D ui_backUp, ui_backDown;
        Vector2 vBackUp, vBackDown;
        Rectangle rcGame;

        // Opening Scene
        Texture2D opening_back;
        Rectangle rcOpening;

        // Main Scene
        Texture2D main_back;
        Rectangle[] rcMainMenu;
        Rectangle rcMain;

        // Seclect Scene
        Texture2D select_back;
        Rectangle rcSelect;

        // Play Scene
        Texture2D play_sea, play_land;                             // 바다와 땅 구분      
        Vector2 vBacksea, vBackland;                              // 배경 화면 Vector2

        // Result Scene
        Texture2D result_back;
        Rectangle rcResult;

        // Rank Scene
        Texture2D rank_back;
        Rectangle rcRank;

        //Help Scene
        Texture2D help_back;
        Rectangle rcHelp;

     
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            
            // 마우스 커서를 보이게 함.
            this.IsMouseVisible = true;

            IsFixedTimeStep = true;
            TargetElapsedTime = new TimeSpan(500000);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            gSetupDevice();
            scene = Game_Scene.OPENING;

            rcGame = new Rectangle(0, 0, 690, 450);
            rcOpening = rcMain = rcSelect = rcResult = rcRank = rcGame;

           
            rcHelp = new Rectangle(0, 0, 690, 450);

            vBackUp = new Vector2(rcGame.Width, 0);
            vBackDown = new Vector2(rcGame.Width, rcGame.Height+390);

            vBacksea = new Vector2(60, 60);
            vBackland = new Vector2(360, 60);

            vObjRock = new Vector2(645, 395);

            base.Initialize();
        }

        // 윈도우창 기본 셋팅
        private void gSetupDevice()
        {
            graphics.PreferredBackBufferWidth = 690;
            graphics.PreferredBackBufferHeight = 450;
            
            graphics.IsFullScreen = false;

            graphics.ApplyChanges();

            Window.Title = "The Isle of East Sea";
        }

        // 마우스 좌표 셋팅
        private void ProcessMouseEvent()
        {
            mouseState = Mouse.GetState();

            // Move the sprite to the current mouse position when the left button is pressed
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
            }
            if (mouseState.RightButton == ButtonState.Pressed &&
                PreviousmouseState.RightButton == ButtonState.Released)
            {

            }     
            // Change the horizontal direction of the sprite when the right mouse button is clicked

            PreviousmouseState = mouseState;
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            // ui scene
            ui_backUp = Content.Load<Texture2D>("images\\ui_backUp");
            ui_backDown = Content.Load<Texture2D>("images\\ui_backDown");

            // Opening Scene
            opening_back = Content.Load<Texture2D>("images\\opening_back");

            // Main Scene
            main_back = Content.Load<Texture2D>("images\\MainBack");

            // Select Scene
            select_back = Content.Load<Texture2D>("images\\select_back");

            // paly scene
            play_land   = Content.Load<Texture2D>("images\\land");
            play_sea = Content.Load<Texture2D>("images\\sea");
            
            //object
            rock = Content.Load<Texture2D>("images\\rock");

            //rank scene
            rank_back = Content.Load<Texture2D>("images\\rank_back");

            //help scene 
            help_back = Content.Load<Texture2D>("images\\help_back");
         
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            Content.Unload();
        }

        // Update

        protected override void Update(GameTime gameTime)
        {


            switch (scene)
            {
                case Game_Scene.OPENING:
                    mouseState = Mouse.GetState();

                    if (mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                    {
                        scene = Game_Scene.MAIN;
                    }

                    PreviousmouseState = mouseState;
                    
                        break;
                case Game_Scene.MAIN:
                        mouseState = Mouse.GetState();
                        if (mouseState.X > 210 && mouseState.X < 440 &&
                            mouseState.Y > 50 && mouseState.Y < 92 &&
                            mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.SELECT;
                        }
                        if (mouseState.X > 210 && mouseState.X < 440 &&
                                mouseState.Y > 160 && mouseState.Y < 196 &&
                                mouseState.LeftButton == ButtonState.Pressed &&
                            PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.RANK;
                        }
                        if (mouseState.X > 210 && mouseState.X < 440 &&
                                   mouseState.Y > 257 && mouseState.Y < 298 &&
                                   mouseState.LeftButton == ButtonState.Pressed &&
                               PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.HELP;
                        }
                        if (mouseState.X > 210 && mouseState.X < 440 &&
                                   mouseState.Y > 338 && mouseState.Y < 378 &&
                                   mouseState.LeftButton == ButtonState.Pressed &&
                               PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            this.Exit();
                        }
                        PreviousmouseState = mouseState;
                        break;
                case Game_Scene.SELECT:
                        mouseState = Mouse.GetState();
                        if (mouseState.X > 0 && mouseState.X < 342 &&
                            mouseState.Y > 0 && mouseState.Y < 197 &&
                            mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.PLAYING;
                        }
                        mouseState = Mouse.GetState();
                        if (mouseState.X > 348 && mouseState.X < 690 &&
                            mouseState.Y > 0 && mouseState.Y < 197 &&
                            mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.PLAYING;
                        }
                        mouseState = Mouse.GetState();
                        if (mouseState.X > 0 && mouseState.X < 342 &&
                            mouseState.Y > 203 && mouseState.Y < 397 &&
                            mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.PLAYING;
                        }
                        mouseState = Mouse.GetState();
                        if (mouseState.X > 348 && mouseState.X < 690 &&
                            mouseState.Y > 203 && mouseState.Y < 397 &&
                            mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.PLAYING;
                        }
                        mouseState = Mouse.GetState();
                        if (mouseState.X > 0 && mouseState.X < 690 &&
                            mouseState.Y > 402 && mouseState.Y < 450 &&
                            mouseState.LeftButton == ButtonState.Pressed &&
                        PreviousmouseState.LeftButton == ButtonState.Released)
                        {
                            scene = Game_Scene.MAIN;
                        }
                    PreviousmouseState = mouseState;
                    break;
                case Game_Scene.PLAYING:
                    mouseState = Mouse.GetState();
                    break;
                case Game_Scene.RESULT:
                    break;
                case Game_Scene.RANK:
                    mouseState = Mouse.GetState();
                    if (mouseState.X > 277 && mouseState.X < 414 &&
                        mouseState.Y > 394 && mouseState.Y < 440 &&
                        mouseState.LeftButton == ButtonState.Pressed &&
                    PreviousmouseState.LeftButton == ButtonState.Released)
                    {
                        scene = Game_Scene.MAIN;
                    }
                    PreviousmouseState = mouseState;
                    break;
                case Game_Scene.HELP:
                    mouseState = Mouse.GetState();
                    if(mouseState.X > 275 && mouseState.X < 412 &&
                        mouseState.Y > 395 && mouseState.Y < 442 &&
                        mouseState.LeftButton == ButtonState.Pressed &&
                    PreviousmouseState.LeftButton == ButtonState.Released)
                    {
                        scene = Game_Scene.MAIN;
                    }
                    PreviousmouseState = mouseState;
                    break;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            // TODO: Add your drawing code here

            switch (scene)
            {
                case Game_Scene.OPENING:
                    spriteBatch.Draw(opening_back, rcGame, rcOpening, Color.White);
                    break;
                case Game_Scene.MAIN:
                    spriteBatch.Draw(main_back, rcMain, Color.White);
                        break;
                case Game_Scene.SELECT:
                    spriteBatch.Draw(select_back, rcSelect, Color.White);
                    break;
                case Game_Scene.PLAYING:
                    spriteBatch.Draw(ui_backUp, vBackUp, Color.White);
                    spriteBatch.Draw(ui_backDown, vBackDown, Color.White );
                    spriteBatch.Draw(play_sea, vBacksea, Color.White);
                    spriteBatch.Draw(play_land, vBackland, Color.White);
                    spriteBatch.Draw(rock, vObjRock, Color.White);
                    break;
                case Game_Scene.RESULT:
                    break;
                case Game_Scene.RANK:
                    spriteBatch.Draw(rank_back, rcRank, Color.White);
                    break;
                case Game_Scene.HELP:
                    spriteBatch.Draw(help_back, rcHelp, Color.White);
                    break;
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
